Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add MySQL column modifier to set current timestamp on update #35143

Merged
merged 3 commits into from
Nov 9, 2020

Conversation

cerbero90
Copy link
Contributor

This PR adds the column modifier useCurrentOnUpdate() to set the current timestamp to a column when a record is updated.

Usage

$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();

This functionality is only supported by MySQL whereas other DBMS require a trigger.

Optional feature
We may also think of updating the Blueprint::timestamps() method to set timestamps automatically, useful for example when we insert/update records via query builder and not through Eloquent:

public function timestamps($precision = 0)
{
    $this->timestamp('created_at', $precision)->useCurrent();

    $this->timestamp('updated_at', $precision)->nullable()->useCurrentOnUpdate();
}

Please let me know what are your thoughts and I'll add it to the PR if it may be useful in your opinion.

If the PR gets merged, I'll make sure to update the docs as well.

@taylorotwell taylorotwell merged commit 4df6994 into laravel:8.x Nov 9, 2020
@cerbero90 cerbero90 deleted the feature/use-current-on-update branch November 9, 2020 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants